﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using DotNetZen.Presentation.Converters;
using System.Diagnostics;
using System.Windows;

namespace Converters
{
    public class TranslationConverter : IValueConverter, IMultiValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                if (value == null || parameter == null)
                    return Binding.DoNothing;

                string text = parameter.ToString();

                if (value.ToString() == "English")
                {
                    switch (text)
                    {
                        case "Format":
                            return "{0}: {1} {2}/{3}";
                        default:
                            return text;
                    }
                }
                else
                {
                    switch (text)
                    {
                        case "Format":
                            return "{0}: {1} {2} per {3}";
                        case "Speed":
                            return "Snelheid";
                        case "Meter":
                            return "Meter";
                        case "Second":
                            return "Seconde";
                        case "Kilometre":
                            return "Kilometer";
                        case "Hour":
                            return "Uur";
                    }
                }

                return Binding.DoNothing;
            }
            catch (Exception ex)
            {
                return DependencyProperty.UnsetValue;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Binding.DoNothing;
        }

        #endregion

        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return this.Convert(values[0], targetType, values[1], culture);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            return new object[] { Binding.DoNothing, Binding.DoNothing };
        }

        #endregion
    }

    public class StringFormatConverter : IMultiValueConverter
    {
        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                return string.Format(values[0].ToString(), values.Skip(1).ToArray());
            }
            catch (Exception ex)
            {
                return DependencyProperty.UnsetValue;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            return new object[targetTypes.Length].FillWith(Binding.DoNothing);
        }

        #endregion
    }

    public class SpeedConverter : IMultiValueConverter
    {
        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                if (values[0] == null || values[1] == null || values[2] == null)
                    return Binding.DoNothing;

                double originalValue = System.Convert.ToDouble(values[0]);
                Distance distance = (Distance)values[1];
                Time time = (Time)values[2];

                double endValue = originalValue;

                // Original is KM/Hour
                if (distance == Distance.Meter)
                    endValue *= 1000;

                if (time == Time.Second)
                    endValue /= 60 * 60;

                return endValue;
            }
            catch (Exception ex)
            {
                return DependencyProperty.UnsetValue;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            return new object[] { Binding.DoNothing, Binding.DoNothing, Binding.DoNothing };
        }

        #endregion
    }
}